﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for NavTab.xaml
    /// </summary>
    public partial class NavTab : UserControl
    {
        public event MouseButtonEventHandler Clicked;
        private bool isSelected = false;
        bool enabled = false;

        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                if (value == true)
                { this.IsEnabled = true; enabled = true; }
                else if (value == false) { this.IsEnabled = false; enabled = false; }
            }
        }

        public NavTab()
        {
            InitializeComponent();
            IsEnabledChanged += new DependencyPropertyChangedEventHandler(NavTab_IsEnabledChanged);
        }

        void NavTab_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == true)
            {
                if (isSelected)
                {
                    TabLabel.Foreground = Brushes.Black;
                }
                else
                {
                    TabLabel.Foreground = Brushes.White;
                }
            }
            if ((bool)e.NewValue == false)
            {
                TabLabel.Foreground = Brushes.DarkGray;
            }
        }

        public UIElement TabPage;

        Brush selectedBgBrush = Brushes.White;
        Brush unSelectedBgBrush = Brushes.Black;
        Brush selectedFgBrush = Brushes.Black;
        Brush unSelectedFgBrush = Brushes.White;

        public Brush BgBrush { get { return unSelectedBgBrush; } set { unSelectedBgBrush = value; if (!isSelected) Tab.Background = value; } }
        public String NavTitle { get { return TabLabel.Content.ToString(); } set { TabLabel.Content = value; } }

        public void Select()
        {
            isSelected = true;
            SelectedOverlay.Visibility = Visibility.Visible;
            TabLabel.FontWeight = FontWeights.Bold;
            TabLabel.Foreground = Brushes.Black;
        }

        public void Unselect()
        {
            isSelected = false;
            SelectedOverlay.Visibility = Visibility.Collapsed;
            TabLabel.FontWeight = FontWeights.Normal;
            if (this.IsEnabled == true)
                TabLabel.Foreground = Brushes.White;
            else
                TabLabel.Foreground = Brushes.DarkGray;
        }

        public void Click(MouseButtonEventArgs _e)
        {
            if (!isSelected)
            {
                Select();
                if (Clicked != null) Clicked(this, _e);
            }
        }

        private void Tab_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!isSelected)
            {
                Select();
                if (Clicked != null) Clicked(this, e);
            }
        }
    }
}
